package com.depin.pdaapp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.depin.pdaapp.R;
import com.depin.pdaapp.event.MyEvent;
import com.depin.pdaapp.presenter.PiciPresenter;
import com.depin.pdaapp.utils.TimeUtil;
import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.basebean.BaseRespose;
import com.jaydenxiao.common.commonutils.KeyBordUtil;
import com.jaydenxiao.common.commonutils.ToastUitl;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

import static com.uuzuche.lib_zxing.activity.CodeUtils.RESULT_SUCCESS;

public class PiciActivity extends BaseActivity<PiciPresenter> implements PiciPresenter.View {


    @BindView(R.id.tv_bar)
    TextView tvBar;
    @BindView(R.id.iv_notification)
    ImageView ivNotification;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_pici)
    EditText etPici;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.lin_time)
    LinearLayout linTime;

    @Override
    public int getLayoutId() {
        return R.layout.activity_pici;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this);
    }

    @Override
    public void initView() {
        tvBar.setText("批次管理");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }

    @OnClick({R.id.iv_back, R.id.lin_time, R.id.btn_center})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_center:
                Intent intent = new Intent(this, ScanActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.lin_time:
                KeyBordUtil.closeKeybord(this);
                TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        String s = TimeUtil.dateToString(date);
                        tvTime.setText(s);
                    }
                })
                        .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                        .setCancelText("取消")//取消按钮文字
                        .setSubmitText("确认")//确认按钮文字
                        .setTitleSize(16)//标题文字大小
                        .setTitleText("时间")//标题文字
                        .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                        .isCyclic(true)//是否循环滚动
                        .setTitleColor(Color.WHITE)//标题文字颜色
                        .setSubmitColor(Color.WHITE)//确定按钮文字颜色
                        .setCancelColor(Color.WHITE)//取消按钮文字颜色
                        .setTitleBgColor(0xFF0371C6)//标题背景颜色 Night mode
                        .setLabel("年", "月", "日", "时", "分", "")//默认设置为年月日时分秒
                        .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                        .isDialog(true)//是否显示为对话框样式
                        .build();
                pvTime.show();
                break;
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsg(MyEvent.MessageEvent event) {
        mPresenter.batch(event.msg, etPici.getText().toString(), tvTime.getText().toString());
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_SUCCESS) {
            String stringExtra = data.getStringExtra(CodeUtils.RESULT_STRING);
            mPresenter.batch(stringExtra, etPici.getText().toString(), tvTime.getText().toString());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void wuliu(BaseRespose baseRespose) {
        ToastUitl.showShort(baseRespose.getMsg());
    }
}